var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var expressJWT = require("express-jwt");

var app = express();
app.use(cors()) 

//配置JWT中间件  认证token
app.use(
  expressJWT({
    secret: 'xiaozz',
    algorithms: ["HS256"],
  }).unless({
    //不需要token认证的路由  登陆不要 注册不要 
    path: ['/login','/count','/addrenwu','/renwulist','/adduser','/uppuser','/addrole','/del/:_id','/addmenu','/quanlist',
    '/userlist','/rolelist','/upload','/addxiaoqu','/xiaoqulist','/addwuzi','/wuzilist','/addcar','/carlist','/addren','/renlist','/chehui/:_id',
    '/jieshou', {
      url: /^\/upload/, methods: ['GET']
    }]
  })
)






// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/upload',express.static(path.join(__dirname, 'upload')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  console.log(err);
});

module.exports = app;
